//
//  PBHomeDownCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/3/14.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBHomeDownCell.h"
#import "PBDownCollectionCell.h"
#import "PBBestHomeModel.h"
#import "PBRentTypeController.h"
#import "PBHomeDetailController.h"

static NSString * const kPBDownCollectionCell = @"PBDownCollectionCell";

@interface PBHomeDownCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray    *dataSource;
@end

@implementation PBHomeDownCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configure];
    [self configureData];
}
- (void)configure {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(220, 200);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 2;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:kPBDownCollectionCell bundle:nil] forCellWithReuseIdentifier:kPBDownCollectionCell];
}
- (void)configureData {
    NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId};
    [TMNetwork requestPostWithParam:param url:@"/house/excellent/query" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            [self.collectionView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)moreButtonClick:(id)sender {
    PBRentTypeController *vc = [[PBRentTypeController alloc] init];
    vc.type = 5;
    vc.hidesBottomBarWhenPushed = YES;
    [self.jumpVC.navigationController pushViewController:vc animated:YES];
}

#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PBDownCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPBDownCollectionCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    PBBestHomeModel *model = self.dataSource[indexPath.row];
    PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
    [self.jumpVC.navigationController pushViewController:vc animated:YES];
}
@end
